import csv

class CSVFile:
	
	csvfile=''

	def openCSVFile(self, filename, delimitador):
		self.csvfile = csv.reader(open(filename), delimiter = delimitador)

	def values(self):
		x = []
		y = []
		for i in self.csvfile:
			x.append( [ float(i[0]) ] )
			y.append( [ float(i[1]) ] )
		return (x,y)

class estatisticas:

	def ordenarDados(self, x, y):
		x.sort()
		y.sort()
		return (x,y)

	def somaDi2(self, x, y):
		i = 0
		SomaDi2 = 0.
		print "X: %s" %x.index(x[3])
		print "Y: %s" %y.index(y[2])
		while (i < len(x)):
			varx = x[i]
			vary = y[i]
			SomaDi2 = SomaDi2 + pow( ( x.index(varx) +1) - ( y.index(vary) +1) , 2 )
			print (x.index(x[i]) +1) - (y.index(y[i]) +1) 
			i= i+1
		return SomaDi2

	def calcularSomaDi2(self, x, y):
		i=0
		somaDi = 0.
		while (i < len(x)):
			somaDi = somaDi + pow(x[i][0]*y[i][0],2)
			i = i+1
			print i
		return somaDi

	def calcularSpearman(self, Di2, N):
		p = 1 - ( (6 * Di2) / (pow(N,3) - N ) )
		return p
		

file = CSVFile()
delimitador = ','

print "Digite o caminho completo do arquivo CSV:" 
path = raw_input()
print 

file.openCSVFile(path, delimitador)

X,Y = file.values()
calculos = estatisticas()
Xs, Ys = calculos.ordenarDados(X,Y)
Di2 = calculos.somaDi2(X,Y)
print "Di2 : %.2f" %Di2 
print calculos.calcularSpearman(Di2, len(X))




